package com.quan.common.config;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;

@Configurable
public class ElasticSearchConfiguration {

	/**
	 * ES 构造
	 * @return
	 * @throws UnknownHostException
	 */
	@Bean
	public TransportClient esClient() throws UnknownHostException {
		Settings settings = Settings.builder()
				.put("cluster.name", "")
				.put("client.transport.sniff", true)
				.build();
		
		InetSocketTransportAddress master = new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300);

		TransportClient client = new PreBuiltTransportClient(settings) ;
		client.addTransportAddress(master) ;
		
		return client ;
	}
	
}
